package com.yidu.hbatsams.utils;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Node implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5805055138870807169L;
	/**
	 * 
	 */
	private String pid; 
	private String id; // 树的节点Id，区别于数据库中保存的数据Id。
	private String name; 
// 点击节点触发的链接
	private List<Node> children = new ArrayList<Node>(); // 子节点，可以用递归的方法读取

	/**
	 * 生成一颗多叉树，根节点为root
	 * 
	 * @param
	 *
	 * @return root
	 */
	public List<Node> createTree(List<Node> nodes) {
		if (nodes == null || nodes.size() < 0)
			return null;
		List<Node> nodeList = new ArrayList<>();// 根节点自定义，但是要和pid对应好
		// 将所有节点添加到多叉树中
		for (Node node : nodes) {
			if (node.getPid().equals("0")) {// 根节点自定义，但是要和pid对应好
				// 向根添加一个节点
				nodeList.add(node);
			} else {
				addChild(nodes, node);
			}
		}
		return nodeList;
	}

	/**
	 * 向指定多叉树节点添加子节点
	 * 
	 * @param
	 *
	 * @param child
	 *            节点
	 */
	public void addChild(List<Node> nodes, Node child) {
		//System.out.println("nodes"+nodes);
		
		for (Node item : nodes) {
			if (item.getId().equals(child.getPid())) {
				// 找到对应的父亲
				//System.out.println("2"+child);	 
				item.getChildren().add(child);
				break;
			}
		}
	}

	public Node() {
	}



	@Override
	public String toString() {
		return "Node [pid=" + pid + ", id=" + id + ", name=" + name + ", children=" + children + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((children == null) ? 0 : children.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((pid == null) ? 0 : pid.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Node other = (Node) obj;
		if (children == null) {
			if (other.children != null)
				return false;
		} else if (!children.equals(other.children))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (pid == null) {
			if (other.pid != null)
				return false;
		} else if (!pid.equals(other.pid))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Node> getChildren() {
		return children;
	}

	public void setChildren(List<Node> children) {
		this.children = children;
	}

	public Node(String pid, String id, String name, List<Node> children) {
		super();
		this.pid = pid;
		this.id = id;
		this.name = name;
		this.children = children;
	}



}

